package com.jacken.mars.service.impl;

import com.jacken.mars.entity.db.GenTable;
import com.jacken.mars.entity.db.vo.TableListVo;
import com.jacken.mars.generator.CodeGenerator;
import com.jacken.mars.mapper.GenTableColumnMapper;
import com.jacken.mars.mapper.GenTableMapper;
import com.jacken.mars.service.IGenTableService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * 业务 服务层实现
 *
 * @author ruoyi
 */
@Service
@AllArgsConstructor
@Slf4j
public class GenTableServiceImpl implements IGenTableService {

    private static Pattern linePattern = Pattern.compile("_(\\w)");

    private final GenTableMapper genTableMapper;


    /**
     * 查询据库列表
     *
     * @param genTable 业务信息
     * @return 数据库表集合
     */
    @Override
    public List<TableListVo> selectDbTableList(GenTable genTable) {
        List<GenTable> genTables = genTableMapper.selectDbTableList(genTable);
        ArrayList<TableListVo> list = new ArrayList<>();
        for (int i = 0; i < genTables.size(); i++) {
            TableListVo vo = new TableListVo();
            vo.setTableId(i + 1);
            vo.setTableName(genTables.get(i).getTableName());
            vo.setTableComment(genTables.get(i).getTableComment());
            vo.setTableEntity(underline2Camel(genTables.get(i).getTableName(), true));
            vo.setCreateTime(genTables.get(i).getCreateTime());
            list.add(vo);
        }
        return list;

    }

    @Override
    public void genCode(String tableName) {
        CodeGenerator.genCode(tableName);
        log.info(tableName + "生成成功!");
    }


    /**
     * 下划线转驼峰
     *
     * @param line
     * @return
     */
    private static String underline2Camel(String line, boolean... firstIsUpperCase) {
        String str = "";

        if (StringUtils.isBlank(line)) {
            return str;
        } else {
            StringBuilder sb = new StringBuilder();
            String[] strArr;
            // 不包含下划线，且第二个参数是空的
            if (!line.contains("_") && firstIsUpperCase.length == 0) {
                sb.append(line.substring(0, 1).toLowerCase()).append(line.substring(1));
                str = sb.toString();
            } else if (!line.contains("_") && firstIsUpperCase.length != 0) {
                if (!firstIsUpperCase[0]) {
                    sb.append(line.substring(0, 1).toLowerCase()).append(line.substring(1));
                    str = sb.toString();
                } else {
                    sb.append(line.substring(0, 1).toUpperCase()).append(line.substring(1));
                    str = sb.toString();
                }
            } else if (line.contains("_") && firstIsUpperCase.length == 0) {
                strArr = line.split("_");
                for (String s : strArr) {
                    sb.append(s.substring(0, 1).toUpperCase()).append(s.substring(1));
                }
                str = sb.toString();
                str = str.substring(0, 1).toLowerCase() + str.substring(1);
            } else if (line.contains("_") && firstIsUpperCase.length != 0) {
                strArr = line.split("_");
                for (String s : strArr) {
                    sb.append(s.substring(0, 1).toUpperCase()).append(s.substring(1));
                }
                if (!firstIsUpperCase[0]) {
                    str = sb.toString();
                    str = str.substring(0, 1).toLowerCase() + str.substring(1);
                } else {
                    str = sb.toString();
                }
            }
        }
        return str;
    }


}